package parts.renderer {
import com.lushu.data.Zone;
import com.lushu.data.ZoneInfo;
import com.lushu.helpers.ZoneReader;
import com.lushu.helpers.structure.TreeNode;
import com.lushu.helpers.structure.ZoneFactory;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;

import mx.collections.ArrayCollection;
import mx.collections.XMLListCollection;
import mx.containers.Canvas;
import mx.containers.HBox;
import mx.containers.VBox;
import mx.controls.Button;
import mx.controls.HRule;
import mx.controls.Label;
import mx.controls.TextInput;
import mx.controls.Tree;
import mx.events.FlexEvent;
import mx.events.ListEvent;

public class ChooseActionBox extends VBox {
    private var titleLabel_:Label;
    private var newMapInput_:TextInput;
    private var newMapButton_:Button;
    private var openMapButton_:Button;
    private var existingMapTree_:Tree;
    private var existingZoneLabel_:Label;

    private var zoneList_:Vector.<ZoneInfo>;
    private var zoneChoosedCallback_:Function;
    private var currentZone_:Zone;

    private var fadeTimer_:Timer;

    public function ChooseActionBox(zoneChoosedCallback:Function) {
        zoneChoosedCallback_ = zoneChoosedCallback;
        addEventListener(FlexEvent.CREATION_COMPLETE, init);
    }


    protected override function createChildren():void {
        super.createChildren();

        titleLabel_ = new Label();
        titleLabel_.text = "Lushu World Designer";
        titleLabel_.percentWidth = 100;
        titleLabel_.setStyle("textAlign", "center");
        titleLabel_.setStyle("fontWeight", "bold");
        titleLabel_.setStyle("fontFamily", "Arial");
        titleLabel_.setStyle("fontSize", "11");
        addChild(titleLabel_);

        var hRule:HRule = new HRule();
        hRule.percentWidth = 100;
        addChild(hRule);

        var lbl:Label = new Label();
        lbl.text = "Créer une nouvelle zone :";
        addChild(lbl);

        var hBox:HBox = new HBox();
        hBox.percentWidth = 100;

        newMapInput_ = new TextInput();
        newMapInput_.width = 328;
        newMapInput_.setStyle("color", "#000000");
        newMapInput_.addEventListener(Event.CHANGE, onTextChanged);
        hBox.addChild(newMapInput_);

        newMapButton_ = new Button();
        newMapButton_.label = "Créer";
        newMapButton_.setStyle("color", "#000000");
        newMapButton_.enabled = false;
        newMapButton_.addEventListener(MouseEvent.CLICK, onNewMapButtonClicked);
        hBox.addChild(newMapButton_);
        addChild(hBox);

        hRule = new HRule();
        hRule.percentWidth = 100;
        addChild(hRule);

        existingZoneLabel_ = new Label();
        existingZoneLabel_.text = "Zone existantes :";
        addChild(existingZoneLabel_);

        openMapButton_ = new Button();
        openMapButton_.label = "Ouvrir";
        openMapButton_.setStyle("color", "#000000");
        openMapButton_.enabled = false;
        openMapButton_.addEventListener(MouseEvent.CLICK, onOpenMapButtonClicked);
        addChild(openMapButton_);

        var canvas:Canvas = new Canvas();
        canvas.percentHeight = 100;
        canvas.percentWidth = 100;

        existingMapTree_ = new Tree();
        existingMapTree_.labelField = "@label";
        existingMapTree_.showRoot = false;
        existingMapTree_.percentWidth = 90;
        existingMapTree_.percentHeight = 100;
        existingMapTree_.setStyle("horizontalCenter", 0);
        existingMapTree_.setStyle("verticalCenter", 0);
        existingMapTree_.addEventListener(ListEvent.ITEM_CLICK, onItemChanged);
        canvas.addChild(existingMapTree_);
        addChild(canvas);
    }

    private function onTextChanged(e:Event):void {
        if (newMapInput_.text != "")
            newMapButton_.enabled = true;
        else
            newMapButton_.enabled = false;
    }

    private function onItemChanged(e:Event):void {
        openMapButton_.enabled = true;
    }

    private function onNewMapButtonClicked(e:MouseEvent):void {
        currentZone_ = ZoneFactory.createZone(newMapInput_.text, zoneChoosedCallback_);
    }

    private function onOpenMapButtonClicked(e:MouseEvent):void {
        var element:Object = existingMapTree_.selectedItem;
        for each(var zoneInfo:ZoneInfo in zoneList_) {
            if (zoneInfo.getName() == element.@data) {
                break;
            }
        }
        currentZone_ = ZoneFactory.createZone(zoneInfo.getName(), zoneChoosedCallback_, zoneInfo);
    }

    public function get currentZone():Zone {
        return currentZone_;
    }

    private function init(e:FlexEvent):void {
        zoneList_ = ZoneReader.getZones();
        if (zoneList_.length == 0)
            existingZoneLabel_.text = "Aucune zone disponible pour le moment";
        var treeDataProvider:ArrayCollection = new ArrayCollection();

        var currentZoneInfo:ZoneInfo;
        var repertoire:Array;
        var subCollection:XMLList

        var rootNode:TreeNode = new TreeNode("root");
        var currentNode:TreeNode;
        var tempNode:TreeNode;
        var found:Boolean;
        var i:int, j:int;
        for (i = 0; i < zoneList_.length; i++) {
            currentZoneInfo = zoneList_[i];

            repertoire = currentZoneInfo.getName().split("/");
            currentNode = rootNode;

            for (j = 1; j < (repertoire.length - 1); j++) {
                tempNode = currentNode.getChildByName(repertoire[j]) as TreeNode;
                if (tempNode == null) {
                    tempNode = new TreeNode(repertoire[j]);
                    currentNode.addChild(tempNode);
                }
                currentNode = tempNode;
            }

            tempNode = new TreeNode(repertoire[0]);
            tempNode.setData(currentZoneInfo);
            currentNode.addChild(tempNode);
        }

        var xml:XMLList = XMLList(rootNode.write());
        existingMapTree_.dataProvider = new XMLListCollection(xml);
    }

    public function fadeAway():void {
        fadeTimer_ = new Timer(50, 10);
        fadeTimer_.addEventListener(TimerEvent.TIMER, onFadeTimer);
        fadeTimer_.addEventListener(TimerEvent.TIMER_COMPLETE, onFadeTimerComplete);
        fadeTimer_.start();
    }

    private function onFadeTimer(e:TimerEvent):void {
        this.alpha -= 0.1;
    }

    private function onFadeTimerComplete(e:TimerEvent):void {
        this.dispatchEvent(new Event("boxRemoved"));
    }
}
}